<?php


namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Material;
use App\Models\Notification;
use App\Models\Work;
use Dingo\Api\Http\Request;

class NotificationController extends BaseController
{

    public function index(Request $request)
    {
        $this->validate($request, [
           'is_system' => 'required|in:0,1'
        ]);
        $input = $request->input();
        $usr_id = $this->getUserId();
        $is_system = $input['is_system'];
        $list = Notification::withCertain('user', ['nickname', 'avatar_url'])
            ->where('is_system', $input['is_system'])->where(function ($q) use($usr_id, $is_system) {
                if ($is_system) {
                    $q->where('entity_user_id', $usr_id)->orWhere('entity_user_id', 0);
                } else {
                    $q->where('entity_user_id', $usr_id);
                }
            })->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));

        foreach ($list as &$item) {
            if ($item->entity_type == 1) {
                $item->entity = Work::where('id', $item->entity_id)->first(['id', 'title', 'image_file', 'video_file']);
            } elseif ($item->entity_type == 2) {
                $item->entity = Material::where('id', $item->entity_id)->first(['id', 'title', 'image_file', 'video_file']);
            }
        }
        return $this->paginate($list);
    }

    public function info(Request $request) {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $info = Notification::find(\request('id'));
        return $this->success($info);
    }
}

